# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2022/12/6 14:42
desc: 专题获客列表页
'''
import time

from appium.webdriver.common.appiumby import AppiumBy

from case.interface.JRJ.Foreground.test_personal_center import f
from proj_spec.CLUE.mobile_po.clue_app_base_page import CLUEAppBasePage


class AccuisitBySubjectListPage(CLUEAppBasePage):

    _district_filter_locator_android = (AppiumBy.XPATH, "//*[@text='地区']")
    _district_filter_locator_ios = (AppiumBy.ACCESSIBILITY_ID, '地区')

    _industry_filter_locator_android = (AppiumBy.XPATH, "//*[@text='行业选择']")
    _industry_filter_locator_ios = (AppiumBy.ACCESSIBILITY_ID, '行业选择')

    _qual_type_filter_locator_android = (AppiumBy.XPATH, "//*[@text='资质类型']")
    _qual_type_filter_locator_ios = (AppiumBy.ACCESSIBILITY_ID, '资质类型')

    _more_filter_locator_android = (AppiumBy.XPATH, "//*[@text='更多筛选']")
    _more_filter_locator_ios = (AppiumBy.ACCESSIBILITY_ID, '更多筛选')

    _reset_btn_locator_android = (AppiumBy.XPATH, "//*[@text='重置']")
    _reset_btn_locator_ios = (AppiumBy.ACCESSIBILITY_ID, '重置')

    _confirm_btn_locator_android = (AppiumBy.XPATH, "//*[@text='确定']")
    _confirm_btn_locator_ios = (AppiumBy.ACCESSIBILITY_ID, '确定')

    def filter_company(self, **kwargs):
        if 'district' in kwargs:
            self.choose_district(kwargs['district'])
        if 'industry' in kwargs:
            self.choose_industry(kwargs['industry'])
        if 'qual_type' in kwargs:
            self.choose_qual_type(kwargs['qual_type'])
        if 'more_filter_type' in kwargs:
            self.choose_more_filters(kwargs['more_filter_type'])

    def reset_filter(self):
        self.find_element_and_click(getattr(self, '_reset_btn_locator_' + self.os))

    def choose_district(self, district):
        """当前第二级行政区仅宁夏回族自治区、青海省、中国台湾省不可滑动，第三级行政区在数量少的情况下不可滑动。第一级行政区为页面上第4个可滚动元素

        :param district:

        :return:
        """
        self.find_element_and_click(getattr(self, '_district_filter_locator_' + self.os))
        # page_source = self.driver.page_source
        district_trail = district.split('/')
        unscrollable_count = 0  # 记录行政区不可滚动的计数
        # 需打开不可见元素
        # self.driver.update_settings({"allowInvisibleElements": True})
        time.sleep(3)


        for i, text in enumerate(district_trail):
            try:
                self.find_element_and_click((AppiumBy.ANDROID_UIAUTOMATOR,
                                             'new UiScrollable(new UiSelector().scrollable(true).instance(%s)).setAsVerticalList().scrollIntoView(new UiSelector().textContains("%s"));'
                                             % (i + 3 - unscrollable_count, text)))
                # self.find_element_and_click((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("%s");' % text))
            except Exception as e:  # unscrollable
                unscrollable_count += 1
                self.find_element_and_click(
                    (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("%s")' % text))
            time.sleep(2)

        self.find_element_and_click(getattr(self, '_confirm_btn_locator_' + self.os))

    def choose_industry(self, industry):
        self.find_element_and_click(getattr(self, '_industry_filter_locator_' + self.os))
        industry_locator = (AppiumBy.XPATH, "//*[@scrollable='true' and not(contains(@class, 'WebView'))]")
        # industry_locator = (AppiumBy.ANDROID_UIAUTOMATOR, "new UiSelector().scrollable(true).instance(2)")
        # self.find_element_and_click((AppiumBy.ANDROID_UIAUTOMATOR, f'new UiScrollable(new UiSelector().scrollable(true).instance(2)).scrollIntoView(new UiSelector().text("{industry}"));'))
        item_locator = (AppiumBy.XPATH, f"//*[not(@text='')]")
        for industry_text in industry:
            element = self.scroll_into_view(industry_locator, item_locator, industry_text)
            assert element, f"找不到指定行业：{industry_text}"
            element.click()
        self.find_element_and_click(getattr(self, '_confirm_btn_locator_' + self.os))

    def choose_qual_type(self, qual_type):
        # 资质类型默认全选，在筛选前先重置
        self.find_element_and_click(getattr(self, '_qual_type_filter_locator_' + self.os))
        self.reset_filter()
        qual_type_locator = (AppiumBy.XPATH, f"//*[@scrollable='true' and not(contains(@class, 'WebView'))]")
        item_locator = (AppiumBy.XPATH, f"//*[not(@text='')]")
        for qual_type_text in qual_type:
            element = self.scroll_into_view(qual_type_locator, item_locator, qual_type_text)
            assert element, f"找不到指定资质类型：{qual_type_text}"
            element.click()
        self.find_element_and_click(getattr(self, '_confirm_btn_locator_' + self.os))

    def choose_more_filters(self, more_filters):
        self.find_element_and_click(getattr(self, '_more_filter_locator_' + self.os))
        more_list = more_filters.values()
        more_filter_locator = (AppiumBy.XPATH, f"//*[@scrollable='true' and not(contains(@class, 'WebView'))]")
        item_locator = (AppiumBy.XPATH, f"//*[not(@text='')]")
        for filter_text in more_list:
            element = self.scroll_into_view(more_filter_locator, item_locator, filter_text)
            assert element, f"找不到指定筛选条件：{filter_text}"
            element.click()
        self.find_element_and_click(getattr(self, '_confirm_btn_locator_' + self.os))